package cn.javacv.gulimall.product.service.impl;

import cn.javacv.gulimall.product.dao.BrandDao;
import cn.javacv.gulimall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.javacv.gulimall.common.utils.PageUtils;
import cn.javacv.gulimall.common.utils.Query;

import cn.javacv.gulimall.product.entity.BrandEntity;
import cn.javacv.gulimall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<BrandEntity> brandEntityQueryWrapper = new QueryWrapper<>();
        Object key = params.get("key");
        if (null!=key && StringUtils.isNotEmpty((String)key)){
            brandEntityQueryWrapper.eq("brand_id",key).or().like("name",key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                brandEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public BrandEntity updateDetail(BrandEntity brandEntity) {
        this.updateById(brandEntity);
        if (StringUtils.isNotEmpty(brandEntity.getName())){
            categoryBrandRelationService.updateBrandRelation(brandEntity.getBrandId(),brandEntity.getName());
            //TODO 以后这里还可能添加其他的
        }
        return null;
    }

}
